import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;

// GUI class, v3.0
public class GUI extends JApplet implements ActionListener
{
	//---------------------------------------------
	// Needed to please eclipse for some reason
	//
	private static final long serialVersionUID = 1L;
	
	//--------------------------------------------
    // combined to create the output window
    //
	JTextPane output;
	JScrollPane scrollPane;
	
	//--------------------------------------------
    // controls declarations
    //
	private JPanel controls;
	private JPanel buttonPanel;
	
	//Mark's entry
	private boolean setXML;
	private JTextField ODBC_entry;
	private JTextArea sqlEntry;
	private JScrollPane jScrollPane1;
	
	private JButton submit;
	private JButton submit2;
	private JButton clearAll;
	
	public void init()
	{		
		this.setSize(new Dimension(560, 560));
	}
	
	public GUI()
	{
		//-------------------------------------------
        // Create the screen
		// notes: 
		// 1. changed layout manager from BorderLayout to GridLayout
		// 2. merged scroll pane and controls to one panel in order to establish self-contained tabbed pane
        JPanel screen = new JPanel(new GridLayout(2, 1)); // grid layout has 2 rows, 1 column
        scrollPane = new JScrollPane();
        output = new JTextPane();
                
        scrollPane.getViewport().add(output);
        
        screen.setBackground(new Color(255, 255, 255));
        screen.setSize(new Dimension(100, 100));
        
        setupControls();

        screen.add(scrollPane); // add scroll pane to first row in grid
        screen.add(controls);   // add controls to second row in grid
        
        //-------------------------------------------
        // Amusing introductory message goes here
        //
        printToScreen("Output, New GUI design\n\n");
        
        //-------------------------------------------
        // Create the top outer panel to hold the tabbed inner panels
        // then add it to the content panel (added by Paul, 6/12)
        JPanel topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);
        
        //-------------------------------------------
        // Get Mark's dbPanel
        //
        JPanel dbPanel = (new GUIDBPanel()).getDBPanel();
        
        //-------------------------------------------
        // Generate tabbed content (added by Paul, 6/12)
        // JTabbedPane.addTab(String, JPanel)
        // * pre : takes a tab name as panel as arguments
        // * post: generates a tab
        //
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("DB Create/Insert", dbPanel);  // add original GUI.java code into first tab
        tabbedPane.addTab("SQL Commands", screen); // add Mark's new DB panel code into second tab
        topPanel.add( tabbedPane, BorderLayout.CENTER );
	}
	
	public void printToScreen(String text)
	{
		try
		{
			output.getDocument().insertString(output.getDocument().getLength(), text, null);
			output.setCaretPosition(output.getDocument().getLength());
		}
		catch (BadLocationException e)
		{
			e.printStackTrace();
		}
	}
	
	//-------------------------------------------
    // Creates all the buttons
    //
	private void setupControls()
	{
		controls = new JPanel();
		buttonPanel = new JPanel();
		
		controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
		//buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		
		//add ODBC box here, Mark's Entry
		JPanel ODBCNamePanel = new JPanel();
		ODBCNamePanel.setBorder(BorderFactory.createTitledBorder("ODBC NAME"));

		ODBC_entry = new JTextField(20);
		ODBCNamePanel.add(ODBC_entry);
				
		controls.add(ODBCNamePanel);
		
		//Mark's entry here
		/*creates the sql entry box also create dimensions
		 * of box.  This will probably need to be edited.
		 */
		JPanel sqlNamePanel = new JPanel();
		sqlNamePanel.setBorder(BorderFactory.createTitledBorder("SQL Entry"));
		
		JPanel sqlIn = new JPanel(new BorderLayout());
        jScrollPane1 = new JScrollPane();
        sqlEntry = new JTextArea();
		sqlEntry.setColumns(30);
        sqlEntry.setRows(5);
                
        jScrollPane1.getViewport().add(sqlEntry);
        
        sqlIn.setBackground(new Color(255, 255, 255));
        sqlIn.setSize(new Dimension(540, 360));
        
        sqlIn.add(jScrollPane1, BorderLayout.CENTER);
				
        sqlNamePanel.add(sqlIn);
        controls.add(sqlNamePanel);
        
        submit = new JButton("Search with XML");
		submit.addActionListener(this);
		
		buttonPanel.add(submit);
		
		submit2 = new JButton("Search without XML");
		submit2.addActionListener(this);
		
		buttonPanel.add(submit2);
		controls.add(buttonPanel);
		
		//create a clear all button to clear the screen
		clearAll = new JButton("Clear All");
		clearAll.addActionListener(this);
		buttonPanel.add(clearAll);
	}
	
	
	public void actionPerformed(ActionEvent action) 
	{
			
		RunDB runDB = new RunDB();
		
		try
		{
			//only run if submit has been pressed.
			//still need to add second button to generate
			//without xml
			if (action.getSource() == submit)
			{
				runDB.loadDriver();
		    	
		    	//need to pass in the name of ODBC here.
		    	runDB.makeConnection(ODBC_entry.getText());
		        runDB.buildStatement();
		        
		        /*ensure that the xml file gets printed to
		         * thus we set the flag to true.
		         */
		        setXML = true;
		        //grab the sql entry from the sql entry table.
		        runDB.executeQuery(this,sqlEntry.getText(),setXML);

			}
			//do this statement if we do not want to generate
			//an xml file
			if (action.getSource() == submit2)
			{
				runDB.loadDriver();
		    	
		    	//need to pass in the name of ODBC here.
		    	runDB.makeConnection(ODBC_entry.getText());
		        runDB.buildStatement();
		        
		        /*ensure that the xml file does not get 
		         * printed to thus we set the flag to false.
		         */
		        setXML = false;
		        //grab the sql entry from the sql entry table.
		        runDB.executeQuery(this,sqlEntry.getText(),setXML);

			}
			//handle clear all button
			if (action.getSource()== clearAll)
			{
				//instead of deleting the ODBC entry just reput
				//what the user entered originally.
				ODBC_entry.setText(ODBC_entry.getText());
				output.setText("");
				sqlEntry.setText("");
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
			//return;
		}
		
	}
}